package org.dev.vlp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dev.vlp.constants.EnumLikeTargetType;
import org.dev.vlp.constants.EnumMomentType;
import org.dev.vlp.constants.EnumNotificationType;
import org.dev.vlp.entity.Comment;
import org.dev.vlp.entity.LikeRelation;
import org.dev.vlp.entity.Moment;
import org.dev.vlp.entity.Notification;
import org.dev.vlp.mapper.CommentMapper;
import org.dev.vlp.mapper.MomentMapper;
import org.dev.vlp.service.LikeService;
import org.dev.vlp.mapper.LikeRelationMapper;
import org.dev.vlp.service.NotificationService;
import org.dev.vlp.utils.AssertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

//import javax.xml.ws.Action;
import java.util.Objects;

/**
* @author 86184
* @description 针对表【vlp_like_relation(点赞-关联表)】的数据库操作Service实现
* @createDate 2023-01-13 17:38:32
*/
@Service
public class LikeServiceImpl extends ServiceImpl<LikeRelationMapper, LikeRelation>
    implements LikeService {

    @Autowired
    private LikeRelationMapper likeRelationMapper;

    @Autowired
    private MomentMapper momentMapper;

    @Autowired
    private NotificationService notificationService;

    @Autowired
    private CommentMapper commentMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void like(EnumLikeTargetType targetType, Long targetId,Long userId) {

        likeRelationMapper.insert(new LikeRelation(userId, targetType.getValue(), targetId));
        if (Objects.equals(targetType, EnumLikeTargetType.MOMENT)) {
            QueryWrapper<Moment> momentQueryWrapper = new QueryWrapper<>();
            momentQueryWrapper.eq("moment_id",targetId).eq("is_deleted",0);
            Moment momentDBO = momentMapper.selectOne(momentQueryWrapper);
            AssertUtils.notNull(momentDBO, "动态不存在：" + targetId);
            momentMapper.updateLikeCount(targetId, 1);

            // 发消息
            notificationService.sendNotification(Notification.builder()
                    .type(EnumNotificationType.LIKE_MOMENT.getValue())
                    .momentId(targetId)
                    .receiveUserId(momentDBO.getUserId())
                    .build(),userId);
//            if (Objects.equals(momentDBO.getType(), EnumMomentType.VIDEO.getValue())) {
//                notificationService.sendNotification(Notification.builder()
//                        .type(EnumNotificationType.LIKE_VIDEO.getValue())
//                        .momentId(targetId)
//                        .receiveUserId(momentDBO.getUserId())
//                        .build(),userId);
//            } else {
//                notificationService.sendNotification(Notification.builder()
//                        .type(EnumNotificationType.LIKE_MOMENT.getValue())
//                        .momentId(targetId)
//                        .receiveUserId(momentDBO.getUserId())
//                        .build(),userId);
//            }
        }

        if (Objects.equals(targetType, EnumLikeTargetType.COMMENT)) {
            QueryWrapper<Comment> commentQueryWrapper = new QueryWrapper<>();
            commentQueryWrapper.eq("comment_id",targetId).eq("is_deleted",0);
            Comment commentDBO = commentMapper.selectOne(commentQueryWrapper);
            AssertUtils.notNull(commentDBO, "评论不存在：" + targetId);
            commentMapper.updateLikeCount(targetId, 1);

            // 发消息
            if (Objects.equals(commentDBO.getParentId(), 0L)) {
                notificationService.sendNotification(Notification.builder()
                        .type(EnumNotificationType.LIKE_COMMENT.getValue())
                        .momentId(commentDBO.getMomentId())
                        .commentId(targetId)
                        .receiveUserId(commentDBO.getUserId())
                        .build(),userId);
            } else {
                if(Objects.equals(commentDBO.getReplyToId(), 0L)){
                    notificationService.sendNotification(Notification.builder()
                            .type(EnumNotificationType.LIKE_CHILDREN_COMMENT.getValue())
                            .momentId(commentDBO.getMomentId())
                            .parentCommentId(commentDBO.getParentId())
                            .commentId(targetId)
                            .receiveUserId(commentDBO.getUserId())
                            .build(),userId);
                }else{
                    notificationService.sendNotification(Notification.builder()
                            .type(EnumNotificationType.LIKE_CHILDREN_COMMENT.getValue())
                            .momentId(commentDBO.getMomentId())
                            .parentCommentId(commentDBO.getReplyToId())
                            .commentId(targetId)
                            .receiveUserId(commentDBO.getUserId())
                            .build(),userId);
                }
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void cancelLike(EnumLikeTargetType targetType, Long targetId,Long userId) {
        QueryWrapper<LikeRelation> likeRelationQueryWrapper = new QueryWrapper<>();
        likeRelationQueryWrapper.eq("user_id",userId).eq("target_id",targetId);
        LikeRelation likeRelation = likeRelationMapper.selectOne(likeRelationQueryWrapper);
        AssertUtils.notNull(likeRelation,"点赞记录不存在");
        likeRelationMapper.delete(likeRelationQueryWrapper);
        if (Objects.equals(targetType, EnumLikeTargetType.MOMENT)) {
            momentMapper.updateLikeCount(targetId, -1);
        }
        if (Objects.equals(targetType, EnumLikeTargetType.COMMENT)) {
            commentMapper.updateLikeCount(targetId, -1);
        }
    }
}




